package cool.taomu.toolkit.platform.controller.api

import cool.taomu.toolkit.service.client.kademlia.service.ServiceClient
import cool.taomu.toolkit.utils.ByteBufferUtils
import io.swagger.annotations.Api
import io.swagger.annotations.ApiOperation
import io.swagger.annotations.ApiParam
import javax.inject.Inject
import javax.servlet.http.HttpServletRequest
import javax.ws.rs.Consumes
import javax.ws.rs.DELETE
import javax.ws.rs.GET
import javax.ws.rs.POST
import javax.ws.rs.PUT
import javax.ws.rs.Path
import javax.ws.rs.Produces
import javax.ws.rs.QueryParam
import javax.ws.rs.core.Context
import javax.ws.rs.core.Response
import org.slf4j.LoggerFactory

@Path("service")
@Api(tags = "服务调用接口")
class ServiceCentreController {

	val static LOG = LoggerFactory.getLogger(ServiceCentreController);

	@Inject
	ServiceClient scservice;

	@GET
	@Path("/node")
	@Produces("application/json")
	@Consumes("application/json")
	@ApiOperation(value = "get类型服务器调用")
	def getService(@ApiParam("服务名") @QueryParam("serviceName") String serviceName, @Context HttpServletRequest request) {
		LOG.info("serviceName:{}", serviceName);
		var entity = scservice.getRequestDto(request);
		var result = scservice.execute(serviceName, entity)
		var r = ByteBufferUtils.toString(result.data,"UTF-8")
		LOG.info(r)
		return Response.ok(r).build();
	}

	@POST
	@Path("/node")
	@Produces("application/json")
	@Consumes("application/json")
	@ApiOperation(value = "post类型服务器调用")
	def postService(@ApiParam("服务名") @QueryParam("serviceName") String serviceName, @Context HttpServletRequest request) {
		LOG.info("serviceName:{}", serviceName);
		var entity = scservice.getRequestDto(request);
        var result = scservice.execute(serviceName, entity)
		return Response.ok(result).build();
	}

	@PUT
	@Path("/node")
	@Produces("application/json")
	@Consumes("application/json")
	@ApiOperation(value = "put类型服务器调用")
	def putService(@ApiParam("服务名") @QueryParam("serviceName") String serviceName, @Context HttpServletRequest request) {
		LOG.info("serviceName:{}", serviceName);
		var entity = scservice.getRequestDto(request);
        var result = scservice.execute(serviceName, entity)
		return Response.ok(result).build();
	}

	@DELETE
	@Path("/node")
	@Produces("application/json")
	@Consumes("application/json")
	@ApiOperation(value = "delete类型服务器调用")
	def deleteService(@ApiParam("服务名")  @QueryParam("serviceName") String serviceName, @Context HttpServletRequest request) {
		LOG.info("serviceName:{}", serviceName);
		var entity = scservice.getRequestDto(request);
        var result = scservice.execute(serviceName, entity)
		return Response.ok(result).build();
	}
}
